import HttpRequestAPI from '@/services'
import type { VideoInfo } from '../home'
import type { ProgressCallback } from '@/services/types/requestType'

//TS 类型声明

/**
 * 分页结果类型
 */
export interface PaginationResultVO<T> {
  totalCount: number
  pageSize: number
  pageNo: number
  pageTotal: number
  list: T[]
}

/**
 * 用户行为类型
 */
export interface UserAction {
  actionId?: number
  videoId: string
  videoUserId: string
  commentId: number
  actionType: number
  actionCount: number
  userId: string
  actionTime: string
  videoCover: string
  videoName: string
}
export interface VideoDetailResultVO {
  videoInfo: VideoInfo
  userActionList: UserAction[]
}

/**
 * 视频弹幕列表类型
 */
export interface Danmu {
  danmuId: number
  videoId: string
  fileId: string
  userId: string
  postTime: string
  text: string
  mode: number
  color: string
  time: number
  nickName: string
  videoName: string
  videoCover: string
}

export type VideoDanmuListVO = Danmu[]

/**
 * 发布弹幕类型
 */
export interface PostDanMuDto {
  videoId?: string
  fileId?: string
  text?: string
  mode?: number
  color?: string
  time?: number
}

/**
 * 视频分P类型信息
 */
export interface VideoP {
  fileId: string
  userId: string
  videoId: string
  fileName: string
  fileIndex: number
  fileSize: number
  filePath: string
  duration: number
}

/**
 * 视频评论类型
 */
export interface VideoComment {
  commentId: number
  pcommentId: number
  videoId: string
  videoUserId: string
  content: string
  imgPath: string
  userId: string
  replyUserId: string
  topType: number
  postTime: string
  likeCount: number
  hateCount: number
  nickName: string
  avatar: string
  replyAvatar: string
  replyNickName: string
  videoName: string
  videoCover: string
  children: VideoComment[]
}

/**
 * 视频评论结果类型
 */
interface VideoCommentResultVO {
  commentData: PaginationResultVO<VideoComment>
  userActionList: UserAction[]
}

/**
 * 获取视频详情信息
 * @returns VideoDetailResultVO
 */
export const videoDetailHttpRequest = (videoId: string | string[]) => {
  return HttpRequestAPI.get<VideoDetailResultVO>({
    url: `/web/video/getVideoInfo?videoId=${videoId}`
  })
}

/**
 * 获取视频弹幕列表
 * @returns VideoDanmuListVO
 */
export const danmuListHttpRequest = (videoId: string | string[], fileId: string) => {
  return HttpRequestAPI.get<VideoDanmuListVO>({
    url: `/interact/danmu/loadDanMu`,
    params: {
      videoId,
      fileId
    }
  })
}

/**
 * 发布弹幕
 * @returns null
 */
export const postDanmuHttpRequest = (data: PostDanMuDto) => {
  return HttpRequestAPI.post<null>({
    url: `/interact/danmu/postDanMu`,
    data
  })
}

/**
 * 获取视频分P列表信息
 * @returns VideoP[]
 */
export const videoPListHttpRequest = (videoId: string | string[]) => {
  return HttpRequestAPI.get<VideoP[]>({
    url: `/web/video/getPVideoInfo?videoId=${videoId}`
  })
}

/**
 * 获取视频在线观看人数
 * @returns VideoP[]
 */
export const reportVideoPlayOnLineHttpRequest = (deviceId: string, fileId: string) => {
  return HttpRequestAPI.get<number>({
    url: `/interact/online/reportVideoPlayOneLine?deviceId=${deviceId}&fileId=${fileId}`
  })
}

/**
 * 视频行为操作(投币、点赞、收藏)
 * @returns
 */
export const VideoDoActionHttpRequest = (data: {
  videoId: string
  actionType: number // 0-4（如点赞、收藏等）
  actionCount?: 1 | 2
  commentId?: number
}) => {
  const formData = new FormData()
  formData.append('videoId', String(data.videoId))
  formData.append('actionType', String(data.actionType))
  formData.append('actionCount', String(data.actionCount || ''))
  formData.append('commentId', String(data.commentId || ''))
  return HttpRequestAPI.post<number>({
    url: `/interact/userAction/doAction`,
    data: formData
  })
}

/**
 * 获取视频评论
 * @returns VideoCommentResultVO
 */
export const videoCommentHttpRequest = (videoId: string, orderType: number, PageNo?: number) => {
  return HttpRequestAPI.get<VideoCommentResultVO>({
    url: `/interact/comment/loadComment?videoId=${videoId}&orderType=${orderType}&PageNo=${PageNo}`
  })
}

/**
 * 发布评论
 * @returns VideoComment
 */
export const postCommentHttpRequest = (data: {
  videoId: string
  content: string
  replyCommentId?: number
  imgPath?: string
}) => {
  const formData = new FormData()
  formData.append('videoId', String(data.videoId))
  formData.append('content', String(data.content))
  formData.append('replyCommentId', String(data.replyCommentId || ''))
  formData.append('imgPath', String(data.imgPath || ''))
  return HttpRequestAPI.post<VideoComment>({
    url: `/interact/comment/postComment`,
    data: formData
  })
}

/**
 * 置顶评论
 * @returns null
 */
export const topCommentHttpRequest = (commentId: number) => {
  return HttpRequestAPI.get<null>({
    url: `/interact/comment/topComment?commentId=${commentId}`
  })
}

/**
 * 取消置顶评论
 * @returns null
 */
export const cancelTopCommentHttpRequest = (commentId: number) => {
  return HttpRequestAPI.get<null>({
    url: `/interact/comment/cancelTopComment?commentId=${commentId}`
  })
}

/**
 * 删除评论
 * @returns null
 */
export const userDelCommentHttpRequest = (commentId: number) => {
  return HttpRequestAPI.delete<null>({
    url: `/interact/comment/userDelComment?commentId=${commentId}`
  })
}

/**
 * 获取相似关键词的视频推荐列表
 * @returns VideoInfo[]
 */
export const getRecommendVideoHttpRequest = (keyword: string, videoId: string) => {
  return HttpRequestAPI.get<VideoInfo[]>({
    url: `/web/video/getRecommendVideo?keyword=${keyword}&videoId=${videoId}`
  })
}

/**
 * 上传封面
 * @returns null
 */
export const uploadImageHttpRequest = (data: { file: File; createIsThumbnail?: boolean }) => {
  console.log(data.file, '@@@@@')
  const formData = new FormData()
  formData.append('file', data.file)
  formData.append('createIsThumbnail', String(data.createIsThumbnail || ''))
  return HttpRequestAPI.post<string>({
    url: `/file/uploadImage`,
    data: formData
  })
}

/**
 * 文件预上传
 * @returns string
 */
export const preUploadVideoHttpRequest = (fileName: string, chunks: number) => {
  const formData = new FormData()
  formData.append('fileName', String(fileName || ''))
  formData.append('chunks', String(chunks || ''))
  return HttpRequestAPI.post<string>({
    url: `/file/preUploadVideo`,
    data: formData
  })
}

/**
 * 视频上传
 * @returns null
 */
export const uploadVideoHttpRequest = (
  data: {
    chunkFile: Blob
    uploadId: string
    chunkIndex: number
  },
  callbacks?: {
    progressCallback?: ProgressCallback
    errorCallback?: (error: any) => void
  }
) => {
  const formData = new FormData()
  formData.append('chunkFile', data.chunkFile)
  formData.append('uploadId', String(data.uploadId || ''))
  formData.append('chunkIndex', String(data.chunkIndex))
  return HttpRequestAPI.postCallback<null>({
    url: `/file/uploadVideo`,
    data: formData,
    uploadProgressCallback: callbacks?.progressCallback
      ? (progress) => callbacks.progressCallback!(progress)
      : undefined,
    errorCallback: callbacks?.errorCallback
  })
}

/**
 * 删除上传的视频
 * @returns string
 */
export const delUploadVideoHttpRequest = (uploadId: string) => {
  return HttpRequestAPI.delete<string>({
    url: `/file/delUploadVideo?uploadId=${uploadId}`
  })
}
